package ru.ds.selenium;

import static org.testng.AssertJUnit.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import ru.ds.selenium.model.Film;
import ru.ds.selenium.model.User;
import ru.ds.selenium.pages.TestBase;

public class CreateDeleteTestNew extends TestBase {
	  List<Film> lstMovies = new ArrayList<Film>();
	
	  @BeforeTest
	  public void prepareTestData() {
		  lstMovies.add(new Film()
		  .setTitle("My new film")
		  .setYear("2012")
		  .setNotes("This movie will be created, should be found and deleted"));
		  
		  lstMovies.add(new Film()
		  .setTitle("")
		  .setYear("")
		  .setNotes("This movie will not be created"));
		  
		  lstMovies.add(new Film()
		  .setTitle("XXX")
		  .setYear("")
		  .setNotes("This movie should not be found!"));
		  
	  }
	  
	  @BeforeMethod
	  public void verifyIfLogoutNeededAndDoLogin() {
          if (app.getUserHelper().isLoggedIn()) {
              app.getUserHelper().logout();
          }
		  User user = new User().setLogin("admin").setPassword("admin");
		  app.getUserHelper().loginAs(user);
	  }
	  //Test of adding movie with all required parameters provided - movie should be added
	  @Test
	  public void test1AddCorrectMovie() {
		  app.getFilmHelper().create(lstMovies.get(0));
		  assertTrue(app.getFilmHelper().isCreated(lstMovies.get(0)));
	  }
	  
	  //Test of adding movie with none of required parameters provided - movie should not be added
	  @Test
	  public void test2AddIncorrectMovie() {
		  app.getFilmHelper().create(lstMovies.get(1));
		  assertFalse(app.getFilmHelper().isCreated(lstMovies.get(1)));
	  }
	  
	  //Test of searching for existing movie - movie should be found
	  @Test
	  public void test3PositiveMovieSearch() {
		  assertTrue(app.getFilmHelper().search(lstMovies.get(0).getTitle()).size() == 1);
	  }
	  
	  //Test of searching for non-existing movie - movie should not be found
	  @Test
	  public void test4NegativeMovieSearch() {
		  assertTrue(app.getFilmHelper().search(lstMovies.get(2).getTitle()).size() == 0);
	  }
	
	  //Test of deletion of existing movie - movie should be deleted
	  @Test
	  public void test5DeleteMovie() {
		  app.getFilmHelper().delete(lstMovies.get(0));
		  app.getFilmHelper().delete(lstMovies.get(1));
		  app.getFilmHelper().delete(lstMovies.get(2));
		  assertTrue(app.getFilmHelper().search(lstMovies.get(0).getTitle()).size() == 0);
	  }

	  @AfterMethod
	  public void doLogout() {
		  app.getUserHelper().logout();
	  }
}
